<template>
	<view class="affiliation">
		<view class="center" v-if="initData">
			<view class="tb">
				<text>上级id:</text>
				{{ initData.agentSn }}
			</view>
			<view class="tb">
				<text>上级名称:</text>
				{{ initData.agentName }}
			</view>
			<view class="tb">
				<text>手机号:</text>
				{{ initData.agentMobile }}
			</view>
			<view class="tb">
				<text>绑定时间:</text>
				{{ initData.agentBindTime }}
			</view>
			<view class="button-list">
				<view class="button" @click="amend">修改代理</view>
				<view class="button white" @click="relieve">解除绑定</view>
			</view>
		</view>

		<uni-popup ref="popup" type="center" maskClick="false">
			<view class="popup">
				<view class="title">绑定代理</view>
				<view class="nr">
					<text>代理商手机号或id：</text>
					<input type="number" v-model="agencyid" placeholder="请输入代理商手机号或id" />
				</view>
				<view class="choose">
					<view class="ok" @click="go_amend">绑定</view>
					<view class="no" @click="close">取消</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import { uniPopup } from '@dcloudio/uni-ui';
import affiliation from '../../../servers/my/affiliation.js';
export default {
	components: { uniPopup },
	data() {
		return {
			agencyid: '',
			initData: ''
		};
	},
	mounted() {
		this.getData();
	},
	methods: {
		getData() {
			affiliation
				.initData()
				.then(res => {
					this.initData = res.data || [];
					var agentSn = this.initData.agentSn;
					if (agentSn && res.code == 200) {
						// affiliation
						// 	.agentById(agentId + '')
						// 	.then(res => {
						// 		this.initData.mobile = res.data.mobile;
						// 	})
						// 	.catch(err => {
						// 		console.log(new Error('请检查网络后重试'));
						// 	});
					} else {
						var _this = this;
						uni.showModal({
							title: '提示',
							content: '没有绑定代理商是否现在绑定',
							success: function(res) {
								if (res.confirm) {
									_this.amend();
								} else if (res.cancel) {
									uni.switchTab({
										url: '/pages/shop/my'
									});
								}
							}
						});
					}
				})
				.catch(err => {
					uni.showToast({
						title: '网络连接失败，请稍后重试',
						icon: 'none',
						duration: 2000,
						success() {
							// setTimeout(function(){uni.switchTab({
							// 	url: '/pages/shop/my'
							// })},2000)
						}
					});
				});
		},
		go_amend() {
			var that = this;
			var obj = { searchParam: this.agencyid };
			affiliation
				.reBind(obj)
				.then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '代理商重新绑定成功',
							duration: 2000,
							success() {
								that.close();
								that.getData(); //数据重新加载
							}
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				})
				.catch(err => {
					console.log(new Error('请检查网络后重试'));
				});

			// affiliation
			// 	.bindSearch(obj)
			// 	.then(res => {
			// 		if (res.data.id == 0) {
			// 			//没有查询到用户
			// 			uni.showToast({
			// 				title: '没有查询到代理商信息，请重新绑定',
			// 				duration: 2000
			// 			});
			// 			return;
			// 		}
			// 		var agent = res.data;
			// 		// var obj = { agentId: agent.id, agentSn: agent.agentSn, agentName: agent.agentName,searchParam:agent.mobile };

			// 	})
			// 	.catch(err => {
			// 		console.log(new Error('请检查网络后重试'));
			// 	});
		},
		amend() {
			this.$refs.popup.open();
		},
		close() {
			this.$refs.popup.close();
			uni.switchTab({
				url: '/pages/shop/my'
			});
		},
		relieve() {
			var that = this;
			uni.showModal({
				title: '提示',
				content: '确定要解除绑定代理',
				success: function(res) {
					if (res.confirm) {
						affiliation
							.unBind()
							.then(res => {
								if (res.code == 200) {
									uni.switchTab({
										url: '/pages/shop/my'
									});
								}
							})
							.catch(err => {
								console.log(new Error('请检查网络后重试'));
							});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
.affiliation {
	.center {
		position: fixed;
		top: 45%;
		left: 0%;
		transform: translate(0, -50%);
		width: 100%;
		.tb {
			padding: 40rpx 40rpx;
			text {
				display: inline-block;
				width: 200rpx;
			}
		}
	}
}

.button-list {
	.button {
		margin: 40rpx 20rpx;
		line-height: 90rpx;
		background: linear-gradient(80deg, #d2158a, #fc6395);
		text-align: center;
		color: #fff;
		border-radius: 45rpx;
		font-size: 32rpx;
		box-shadow: 3rpx 0 20rpx rgba(206, 205, 204, 0.35);
	}
	.white {
		color: #666;
		background: #fff;
		border: 1px solid #d5d5d5;
	}
}
.popup {
	width: 700rpx;
	background: #ffffff;
	text-align: center;
	line-height: 80rpx;
	border-radius: 10rpx;
	.title {
		line-height: 130rpx;
	}
	.nr {
		display: flex;
		width: 93%;
		margin: 0 auto;
		text {
			width: 40%;
			font-size: 28rpx;
		}
		input {
			width: 60%;
			display: block;
			height: 80rpx;
			text-align: left;
			border: 1px solid #efefef;
			border-radius: 5rpx;
			padding-left: 20rpx;
			font-size: 28rpx;
		}
	}
	.choose {
		padding: 40rpx 0;
		view {
			display: inline-block;
			width: 40%;
			margin: 0 3%;
			border-radius: 10rpx;
			box-sizing: border-box;
		}
		.ok {
			background: #d2158a;
			color: #fff;
		}
		.no {
			border: 1px solid #efefef;
		}
	}
}
</style>
